"use strict";

const { Service } = require("egg");

class CodeService extends Service {
    /**
     * 保存手机验证码
     * @param {string} mobile 手机号
     * @param {string} code 手机验证码
     */
    async create(mobile, code) {
        let sql = "insert into code (mobile, code) values (?, ?)";
        let params = [mobile, code];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }

    /**
     * 查询数据库中的验证码
     * @param {string} mobile 手机号
     */
    async find(mobile) {
        let sql = "select * from code where mobile = ?";
        let params = [mobile];
        let res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    /**
     * 删除旧的验证码
     * @param {string} mobile 手机号
     */
    async remove(mobile) {
        let sql = "delete from code where mobile = ?";
        let params = [mobile];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = CodeService;
